/* global window */
import modelExtend from 'dva-model-extend'
import { list, query, create, update, remove, patchremove, setAvailable } from 'services/basicData'
import queryString from 'query-string'
import * as basicData from 'services/basicData.js'
import { message } from 'antd'
import { pageModel } from './common'
import { list as exchangesetList } from '../services/exchangeset'

export default modelExtend(pageModel, {
  namespace: 'basicData',

  state: {
    searchType: '',
    currenciesTypeData: [],
    modalState: 'noOperation',
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/basicdata') {
          let payload
          if (location.search === '') {
            payload = { page: '1', type: 'FundsType' }
          } else {
            payload = queryString.parse(location.search) || { page: 1 }
          }
          if (!('type' in payload)) {
            payload = { ...payload, type: 'FundsType' }
          }
          dispatch({
            type: 'list',
            payload,
          })
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      payload.used = '0'
      const data = yield call(list, payload)
      let dataExchangesetPageOne = {}
      let dataExchangesetPageTwo = {}
      let dataExchangeset = []
      if (payload.type === 'Currencies') {
        dataExchangesetPageOne = yield call(exchangesetList, { page: 1, pageSize: 10 })
        dataExchangesetPageTwo = yield call(exchangesetList, { page: 2, pageSize: 10 })
        dataExchangeset = (dataExchangesetPageOne.data.data).concat(dataExchangesetPageTwo.data.data)
      }
      const listData = data.data
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
      if (dataExchangeset) {
        yield put({
          type: 'updateState',
          payload: {
            searchType: payload.type,
            currenciesTypeData: dataExchangeset,
          },
        })
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, payload)
      const { selectedRowKeys } = yield select(_ => _.basicData)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload), modalState: 'deleteSuccess' } })
        yield put({ type: 'list', payload })
        message.success('delete success')
      } else {
        message.error('delete failed')
        yield put({ type: 'updateState', payload: { modalState: 'deleteFailed' } })
        throw data
      }
    },

    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(patchremove, payload)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: [], modalState: 'multiDeleteSuccess' } })
        yield put({ type: 'list', payload: { type: payload.type } })
      } else {
        message.error('delete failed')
        yield put({ type: 'updateState', payload: { modalState: 'multiDeleteFailed' } })
        throw data
      }
    },

    * create ({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list', payload: { type: payload.type } })
        yield put({ type: 'updateState', payload: { modalState: 'createSuccess' } })
      } else {
        message.error('create failed')
        yield put({ type: 'updateState', payload: { modalState: 'createFailed' } })
        throw data
      }
    },

    * setAvailable ({ payload }, { call, put }) {
      const data = yield call(setAvailable, payload)
      if (data.success) {
        message.success('Set success')
        yield put({ type: 'list', payload: { type: payload.type } })
      } else {
        message.error('Set failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const id = yield select(({ basicData }) => basicData.currentItem.id)
      const type = yield select(({ basicData }) => basicData.currentItem.type)
      const newData = { ...payload, id, type }
      const data = yield call(update, newData)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list', payload: { type } })
        yield put({ type: 'updateState', payload: { modalState: 'updateSuccess' } })
      } else {
        message.error('update failed')
        yield put({ type: 'updateState', payload: { modalState: 'updateFailed' } })
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      let sendData = {}
      sendData.id = payload.currentItem.id
      sendData.type = payload.currentItem.type
      const data = yield call(query, sendData)
      if (data.success) {
        yield put({
          type: 'updateModalState',
          payload: {
            modalType: 'update',
            currentItem: data.data,
            modalVisible: true,
          },
        })
      } else {
        throw data
      }
    },

  },

  reducers: {
  },
})
